type GPSPoint = { lat: number; lon: number };
type PagePoint = { x: number; y: number };

class GPS2PageTransformer {
  private anchorGPSPts: GPSPoint[];
  private anchorPagePts: PagePoint[];

  constructor(anchorGPSPts: GPSPoint[], anchorPagePts: PagePoint[]) {
    console.log('anchorGPSPts', anchorGPSPts);
    console.log('anchorPagePts', anchorPagePts);
    this.anchorGPSPts = anchorGPSPts;
    this.anchorPagePts = anchorPagePts;
  }

  private linearInterpolate(p0: number, p1: number, t: number): number {
    return p0 + (p1 - p0) * t;
  }

  public transform(gps: GPSPoint): PagePoint {
    // 简单起见，此处假设 GPS -> 页面坐标的转换是线性的，即实际情况可能不是
    // 你可能需要一个复杂的地理坐标到像素坐标的转换算法
    const latRange = this.anchorGPSPts[1].lat - this.anchorGPSPts[0].lat;
    const lonRange = this.anchorGPSPts[1].lon - this.anchorGPSPts[0].lon;
    const xRange = this.anchorPagePts[1].x - this.anchorPagePts[0].x;
    const yRange = this.anchorPagePts[1].y - this.anchorPagePts[0].y;

    const latT = (gps.lat - this.anchorGPSPts[0].lat) / latRange;
    const lonT = (gps.lon - this.anchorGPSPts[0].lon) / lonRange;

    return {
      x: this.linearInterpolate(this.anchorPagePts[0].x, this.anchorPagePts[1].x, lonT),
      y: this.linearInterpolate(this.anchorPagePts[0].y, this.anchorPagePts[1].y, latT)
    };
  }
}

const convert2Page = (gpsPoints: GPSPoint[], pagePoints: PagePoint[], gpsValue: GPSPoint) => {
  const transformer = new GPS2PageTransformer(gpsPoints, pagePoints);
  // 转换一个 GPS 点到页面坐标
  return transformer.transform(gpsValue);
};

export default convert2Page;
